package com.offroader.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.offroader.R;
import com.offroader.core.OffRoaderApp;

public class ViewUtils {

//	public static View inflateView(Context ctx, int resId) {
//		return View.inflate(ctx, resId, null);
//	}
	
	@SuppressWarnings("unchecked")
	public static <T> T inflateView(Context ctx, int resId) {
		return (T)View.inflate(ctx, resId, null);
	}

	public static void toast(int resId) {
		toast(OffRoaderApp.getInstance().getString(resId));
	}

	public static void toast(String msg) {
		Toast.makeText(OffRoaderApp.getInstance(), msg, Toast.LENGTH_SHORT).show();
	}
	
	/**
	 * 提示对话框
	 * @param act
	 * @param msg
	 * @param pListener
	 */
	public static void alert(Activity act, String msg, DialogInterface.OnClickListener pListener) {

		String title = act.getString(R.string.dialog_confirm_title);
		String pBtnMsg = act.getString(R.string.dialog_confirm_btn_positive);
		String nBtnMsg = act.getString(R.string.dialog_confirm_btn_negative);

		alert(act, title, msg, pBtnMsg, pListener);

	}
	
	/**
	 * 确认对话框
	 * @param act
	 * @param msg
	 * @param pListener
	 */
	public static void confirm(Activity act, String msg, DialogInterface.OnClickListener pListener) {

		String title = act.getString(R.string.dialog_confirm_title);
		String pBtnMsg = act.getString(R.string.dialog_confirm_btn_positive);
		String nBtnMsg = act.getString(R.string.dialog_confirm_btn_negative);

		confirm(act, title, msg, pBtnMsg, nBtnMsg, pListener, null);

	}
	
	public static void alert(Activity act, String title, String msg, String pBtnMsg, DialogInterface.OnClickListener pListener) {

		new AlertDialog.Builder(act).setTitle(title)//
				.setMessage(msg)//
				.setPositiveButton(pBtnMsg, pListener)//
				.create()//
				.show();

	}

	public static void confirm(Activity act, String title, String msg, String pBtnMsg, String nBtnMsg, DialogInterface.OnClickListener pListener,
			DialogInterface.OnClickListener nListener) {

		new AlertDialog.Builder(act).setTitle(title)//
				.setMessage(msg)//
				.setNegativeButton(nBtnMsg, nListener)//
				.setPositiveButton(pBtnMsg, pListener)//
				.create()//
				.show();

	}

	/**
	 * 自定义对话框
	 * @param ctx
	 * @param layoutResId
	 * @return
	 */
	public static Dialog custom(Context ctx, int layoutResId) {
		Dialog dialog = new Dialog(ctx, R.style.Theme_FullHeightDialog);
		dialog.setContentView(layoutResId);

		return dialog;
	}

	/**
	 * 自定义对话框（底部弹出动画）
	 * @param ctx
	 * @param layoutResId
	 * @return
	 */
	public static Dialog customBottomAnim(Context ctx, int layoutResId) {
		Dialog dialog = ViewUtils.custom(ctx, layoutResId);

		//设置属性
		WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
		int[] pixs = PhoneUtils.getScreenPix(OffRoaderApp.getInstance());
		lp.width = (int) (pixs[0]);
		lp.gravity = Gravity.BOTTOM;

		dialog.onWindowAttributesChanged(lp);
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(true);
		dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画

		return dialog;
	}

	/**
	 * 数据加载
	 * @param act
	 * @return
	 */
	public static Dialog progressWaitting(Fragment frag) {
		if (frag == null || !frag.isAdded()) {
			return null;
		}

		return progressWaitting(frag.getActivity(), true);
	}

	/**
	 * 数据加载
	 * @param act
	 * @return
	 */
	public static Dialog progressWaitting(final Context act) {

		return progressWaitting(act, true);
	}

	/**
	 * 数据加载
	 * @param act
	 * @return
	 */
	public static Dialog progressWaitting(final Context ctx, boolean canceledOnTouch) {
		if (ctx == null) {
			return null;
		}

		LayoutInflater inflater = LayoutInflater.from(ctx);
		View v = inflater.inflate(R.layout.dialog_wait, null);// 得到加载view  
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局  
		// main.xml中的ImageView  
		ImageView spaceshipImage = (ImageView) v.findViewById(R.id.dialog_wait_img);
		TextView tipTextView = (TextView) v.findViewById(R.id.dialog_wait_img_text);// 提示文字  
		// 加载动画  
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(ctx, R.anim.progress_wait);
		// 使用ImageView显示动画  
		spaceshipImage.startAnimation(hyperspaceJumpAnimation);

		tipTextView.setText("请等待...");// 设置加载信息  

		Dialog dialog = new Dialog(ctx, R.style.progress_waitting_circle);// 创建自定义样式dialog  

		dialog.setCancelable(true);// 不可以用“返回键”取消  
		dialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局

		return dialog;
	}

	public static void setRadiusBg(View v, String color) {
		//设置颜色 和圆角
		int strokeWidth = 0; // 边框宽度
		int roundRadius = 12; // 圆角半径
		int strokeColor = Color.parseColor(color);//边框颜色
		int fillColor = Color.parseColor(color);//内部填充颜色

		GradientDrawable gd = new GradientDrawable();//创建drawable
		gd.setColor(fillColor);
		gd.setCornerRadius(roundRadius);
		gd.setStroke(strokeWidth, strokeColor);

		v.setBackgroundDrawable(gd);
	}

	/**
	 * 对TextView设置不同状态时其文字颜色。
	 * 
	 * @param normal
	 * @param pressed
	 * @param focused
	 * @param unable
	 * @return
	 */
	public static ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
		int[] colors = new int[] { pressed, focused, normal, focused, unable, normal };
		int[][] states = new int[6][];
		states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
		states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
		states[2] = new int[] { android.R.attr.state_enabled };
		states[3] = new int[] { android.R.attr.state_focused };
		states[4] = new int[] { android.R.attr.state_window_focused };
		states[5] = new int[] {};
		ColorStateList colorList = new ColorStateList(states, colors);
		return colorList;
	}

	/** 设置Selector。 */
	public static StateListDrawable newSelector(Context context, int idNormal, int idPressed, int idFocused, int idUnable) {
		StateListDrawable bg = new StateListDrawable();
		Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
		Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
		Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
		Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable);
		// View.PRESSED_ENABLED_STATE_SET  
		bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
		// View.ENABLED_FOCUSED_STATE_SET  
		bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
		// View.ENABLED_STATE_SET  
		bg.addState(new int[] { android.R.attr.state_enabled }, normal);
		// View.FOCUSED_STATE_SET  
		bg.addState(new int[] { android.R.attr.state_focused }, focused);
		// View.WINDOW_FOCUSED_STATE_SET  
		bg.addState(new int[] { android.R.attr.state_window_focused }, unable);
		// View.EMPTY_STATE_SET  
		bg.addState(new int[] {}, normal);
		return bg;
	}

}
